package com.iut.projectandroid;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.iut.model.BusinessLayer;
import com.iut.model.Parcelle;
import com.iut.utils.MenuHelper;

public class ParcelDetailActivity extends Activity {

	protected static final String PARCEL_ID = "PARCEL_ID";
	
	private ImageView imageView;
	
	private GoogleMap map;
	
	private DrawerLayout drawerLayout;
	
	private MapFragment mapFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_parcel_detail);
		
		drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		
		mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.google_map_frag);
		
		int id = getIntent().getIntExtra(PARCEL_ID, -1);
		
		final Parcelle parcelle = BusinessLayer.parcelleDetail(getApplicationContext(), Parcelle.class, id);
		
		drawerLayout.setDrawerListener(new DrawerListener() {
			
			@Override
			public void onDrawerStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onDrawerSlide(View arg0, float arg1) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onDrawerOpened(View arg0) 
			{
				map = ((MapFragment)getFragmentManager().findFragmentById(R.id.google_map_frag)).getMap();
				
				LatLng position = new LatLng(parcelle.getLatitude(), parcelle.getLongitude());
				
				CameraUpdate camUpdate = CameraUpdateFactory
						.newLatLngZoom(position, 15);
				
				map.addMarker(new MarkerOptions().position(position));
				
				map.moveCamera(camUpdate);
			}
			
			@Override
			public void onDrawerClosed(View arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		imageView = (ImageView) findViewById(R.id.image);
		
		setImageViewContent(parcelle.getImage());
		
		TextView name = (TextView) findViewById(R.id.name_parcel_detail);
		
		TextView culture = (TextView) findViewById(R.id.culture_parcel_detail);
		
		TextView culturePrec = (TextView) findViewById(R.id.culture_prec_parcel_detail);
		
		TextView surface = (TextView) findViewById(R.id.surface_parcel_detail);
		
		name.setText(parcelle.getName());
		culture.setText(parcelle.getCulture());
		culturePrec.setText(parcelle.getCulturePrecedente());
		surface.setText(parcelle.getSurface());
	}

	private void setImageViewContent(String image) 
	{
		imageView.setImageDrawable(Drawable.createFromPath(image));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.alternative, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if(MenuHelper.launchActivity(this ,getApplicationContext(), item.getItemId()))
		{
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	protected void onPause() 
	{
		
		super.onPause();
	}
}
